﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Nivdal.Sponsors
{
    public partial class ChooseCountries : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GetSponsorToCountriesSqlDataSource.SelectParameters.Add("SponsorId", Session["SponsorId"].ToString());
                //this.MessageLabel.Text = Session["SponsorId"].ToString();
            }
            //int x = this.SponsorToCountriesRepeater.Items[RepeaterItems.count - 1];

            //SponsorToCountriesRepeater.items[repeater.items.count - 1];

        }



        protected void RegionCheckBox_OnCheckedChanged(object sender, EventArgs e)
        {

            CheckBox chbActive = (CheckBox)sender;
            //this.MessageLabel.Text = chbActive.Checked.ToString();
            //this.MessageLabel.Text = chbActive.ToolTip;
            foreach (RepeaterItem item in SponsorToCountriesRepeater.Items)
            {
                CheckBox CountryIdCheckBox = (CheckBox)item.FindControl("CountriesCheckBox");
                if (CountryIdCheckBox != null)
                {
                    if (chbActive.ToolTip == CountryIdCheckBox.ToolTip)
                    {
                        if (chbActive.Checked)
                            CountryIdCheckBox.Checked = true;
                        if (!chbActive.Checked)
                            CountryIdCheckBox.Checked = false;
                    }
                            
                }

            }

            //HiddenField RegionId = (HiddenField)Repeater1.FindControl("RegionIdHiddenField");
            //this.MessageLabel.Text = RegionId.Value.ToString();
           

        }

        protected void SubmitButton_Clicked(object sender, EventArgs e)
        {
            string CountryId = "";
            bool IsEnabled = false;
            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_NewSponsorSetCountryConnection";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            foreach (RepeaterItem item in SponsorToCountriesRepeater.Items)
            {
                CheckBox CountryIdCheckBox = (CheckBox)item.FindControl("CountriesCheckBox");
                HiddenField CountryHiddenId = (HiddenField)item.FindControl("CountryIdHiddenField");

                if (CountryIdCheckBox != null)
                {
                    CountryId = CountryHiddenId.Value.ToString();
                    IsEnabled = CountryIdCheckBox.Checked;
                }

                SqlParameter dbParam_SponsorId = new SqlParameter();
                dbParam_SponsorId.ParameterName = "@SponsorId";
                dbParam_SponsorId.Value = Session["SponsorId"].ToString();
                dbParam_SponsorId.DbType = DbType.String;
                dbParam_SponsorId.Size = 64;
                dbCommand.Parameters.Add(dbParam_SponsorId);

                SqlParameter dbParam_CountryId = new SqlParameter();
                dbParam_CountryId.ParameterName = "@CountryId";
                dbParam_CountryId.Value = CountryId;
                dbParam_CountryId.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_CountryId);

                SqlParameter dbParam_IsEnabled = new SqlParameter();
                dbParam_IsEnabled.ParameterName = "@IsEnabled";
                dbParam_IsEnabled.Value = IsEnabled;
                dbParam_IsEnabled.DbType = DbType.Boolean;
                dbCommand.Parameters.Add(dbParam_IsEnabled);
                try
                {

                    dbConnection.Open();
                    dbCommand.ExecuteScalar();
                    dbCommand.Parameters.Remove(dbParam_SponsorId);
                    dbCommand.Parameters.Remove(dbParam_CountryId);
                    dbCommand.Parameters.Remove(dbParam_IsEnabled);
                }
                catch (Exception SQLDBException)
                {
                    this.MessageLabel.Text = Convert.ToString(SQLDBException);
                    return;
                }


                finally
                {
                    dbConnection.Close();
                }


            }
        }

       

        //protected void ChooseCountryButton_Click(string RegionId)
        //{
        //    this.MessageLabel.Text = RegionId;

            //foreach (RepeaterItem item in Repeater1.Items)
            //{
            //    HiddenField RegionId = (HiddenField)item.FindControl("RegionIdHiddenField");
            //    if (RegionId != null)
            //    {
            //        this.MessageLabel.Text = RegionId.Value.ToString();
            //    }
               
            //}

        //}
    }
}